package com.client.controller;

import com.client.pojo.SecurityQuestions;
import com.client.service.SecurityQuestionsService;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户密保问题表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
@RequestMapping("/securityQuestions")
public class SecurityQuestionsController {
    @Autowired
    SecurityQuestionsService securityQuestionsService;

    /**
     * 保存用户的密保问题
     * @param questions 问题和答案的数组
     * @return 响应消息
     */
    @PostMapping("setQuestions")
    public ResponseMsg setQuestions(@RequestBody SecurityQuestions[] questions){
        if(questions == null || questions.length!=3){
            return ResponseMsg.getInstance(ResponseMsg.ERROR,"请正确写入三个问题");
        }
        for(SecurityQuestions s : questions){
            if(s.getAnswer().isBlank()){
                return ResponseMsg.getInstance(ResponseMsg.ERROR,"问题【"+s.getQuestion()+"】的答案不能为空！");
            }
            securityQuestionsService.save(s);
            System.out.println(s);
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"设置成功！");
    }
    @GetMapping("getCount/{userId}")
    public ResponseMsg getCount(@PathVariable("userId") Integer userId){
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"查询用户密保问题数量",
                securityQuestionsService.countById(userId));
    }
}
